package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IAppmanagerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IAlarmtypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目报警等级Controller
 * 
 * @author ruoyi
 * @date 2024-02-26
 */
@RestController
@RequestMapping("/system/alarmtype")
public class AlarmtypeController extends BaseController
{
    @Autowired
    private IAlarmtypeService alarmtypeService;
    @Autowired
    private IAppmanagerService appManagerService;

    /**
     * 查询项目报警等级列表
     */
    @PreAuthorize("@ss.hasPermi('system:alarmtype:list')")
    @GetMapping("/list")
    public TableDataInfo list(Alarmtype alarmtype,@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return getDataTable(new ArrayList<>());
        }
        startPage();
        List<Alarmtype> list = alarmtypeService.selectAlarmtypeList(alarmtype,dbName);
        return getDataTable(list);
    }

    /**
     * 不分页，查询驱动列表
     */
    @PreAuthorize("@ss.hasPermi('system:drllinfo:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return success(new ArrayList<>());
        }
        List<Alarmtype> list = alarmtypeService.selectAlarmtypeForAll(dbName);
        return success(list);
    }

    /**
     * 导出项目报警等级列表
     */
    @PreAuthorize("@ss.hasPermi('system:alarmtype:export')")
    @Log(title = "项目报警等级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Alarmtype alarmtype,@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        List<Alarmtype> list = new ArrayList<>();
        if(appList != null && appList.size() > 0){
            list = alarmtypeService.selectAlarmtypeList(alarmtype,dbName);
        }
        ExcelUtil<Alarmtype> util = new ExcelUtil<Alarmtype>(Alarmtype.class);
        util.exportExcel(response, list, "项目报警等级数据");
    }

    @Log(title = "项目报警等级", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:alarmtype:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, @RequestParam String dbName) throws Exception
    {
        ExcelUtil<Alarmtype> util = new ExcelUtil<Alarmtype>(Alarmtype.class);
        List<Alarmtype> alarmTypeList = util.importExcel(file.getInputStream());
        String operaName = getUsername();
        String message = alarmtypeService.importAlarmType(alarmTypeList, updateSupport, operaName, dbName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Alarmtype> util = new ExcelUtil<Alarmtype>(Alarmtype.class);
        util.importTemplateExcel(response, "项目报警等级数据");
    }

    /**
     * 获取项目报警等级详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:alarmtype:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id,@RequestParam String dbName)
    {
        return success(alarmtypeService.selectAlarmtypeById(id,dbName));
    }

    /**
     * 新增项目报警等级
     */
    @PreAuthorize("@ss.hasPermi('system:alarmtype:add')")
    @Log(title = "项目报警等级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Alarmtype alarmtype,@RequestParam String dbName)
    {
        return toAjax(alarmtypeService.insertAlarmtype(alarmtype,dbName));
    }

    /**
     * 修改项目报警等级
     */
    @PreAuthorize("@ss.hasPermi('system:alarmtype:edit')")
    @Log(title = "项目报警等级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Alarmtype alarmtype,@RequestParam String dbName)
    {
        return toAjax(alarmtypeService.updateAlarmtype(alarmtype,dbName));
    }

    /**
     * 删除项目报警等级
     */
    @PreAuthorize("@ss.hasPermi('system:alarmtype:remove')")
    @Log(title = "项目报警等级", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids,@RequestParam String dbName)
    {
        return toAjax(alarmtypeService.deleteAlarmtypeByIds(ids,dbName));
    }
}
